package com.kasax.httpclient.beans;

import android.text.TextUtils;

import com.kasax.httpclient.HttpUrl;
import com.kasax.httpclient.utils.LogUtils;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

public class Request {

    // 请求头
    Map<String, String> headerMap;
    // url
//    String url;
    // 路径参数:GET
    Map<String, String> pathParmMap;
    // 查询参数:Get
    Map<String, String> queryParmaMap;
    // 请求类型
    String methodName;
    RequestBody body;
    // 解析url成url对象
    HttpUrl url;

    public Request(Builder builder) {
        this.headerMap = builder.headerMap;
        this.pathParmMap = builder.pathParmMap;
        this.queryParmaMap = builder.queryParmaMap;
        this.methodName = builder.methodName;
        this.body = builder.body;
        this.url = builder.url;
    }

    public Map<String, String> headers() {
        return headerMap;
    }

    public RequestBody body() {
        return body;
    }

    public HttpUrl url() {
        return url;
    }

    public String method() {
        return methodName;
    }

    public static class Builder {
        // 请求头
        private Map<String, String> headerMap = new HashMap<>();
        // url
//        private String url;
        // 路径参数:GET
        private Map<String, String> pathParmMap = new HashMap<>();
        // 查询参数:Get
        private Map<String, String> queryParmaMap = new HashMap<>();
        // 请求类型
        private String methodName;

        // 请求体
        private RequestBody body;

        // url
        private HttpUrl url;


        public Builder url(String url) {
            try {
                this.url = new HttpUrl(url);
            } catch (MalformedURLException e) {
                LogUtils.e(getClass(), e);
            }
            return this;
        }

        public Builder get() {
            this.methodName = "GET";
            return this;
        }

        public Builder post() {
            this.methodName = "POST";
            return this;
        }

        public Builder body(RequestBody body) {
            this.body = body;
            return this;
        }


        public Builder addRequestParam(Map<String, String> map) {
            queryParmaMap.putAll(map);
            return this;
        }

        public Builder addPathParam(Map<String, String> map) {
            queryParmaMap.putAll(map);
            return this;
        }

        public Builder addHeader(String key, String value) {
            headerMap.put(key, value);
            return this;
        }

        public Builder readHeader(String key) {
            headerMap.remove(key);
            return this;
        }

        public Builder addHeaders(Map<String, String> map) {
            headerMap.putAll(map);
            return this;
        }

        public Request build() {
            if (url == null) {
                throw new IllegalStateException("url is null");
            }
            if (TextUtils.isEmpty(methodName)) {
                // 默认是GET
                methodName = "GET";
            }
            return new Request(this);
        }

    }


}
